package net.ipinglun.module.main;

import android.content.Intent;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.andexert.library.RippleView;

import net.ipinglun.R;
import net.ipinglun.module.newsList.NewsFragment;
import net.ipinglun.util.Tools;

import java.util.ArrayList;


public class MainActivity extends FragmentActivity {

    private DrawerLayout mDrawerLayout;
    private RippleView mMenuRippleView;
    private View mIndicator;
    private int mIndicatorWidth;
    private RelativeLayout.LayoutParams mIndicatorParams;

    private TextView mChannelTextView0;
    private TextView mChannelTextView1;

    private ViewPager mViewPager;
    private NewsFragmentPagerAdapter mFragmentPagerAdapter;
    private ArrayList<NewsFragment> mArrayListFragment = new ArrayList<NewsFragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明状态栏
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); //透明导航栏
        }

        for (int i = 0; i < 2; i++) {
            NewsFragment tempFragment = NewsFragment.newInstance(i);
            mArrayListFragment.add(tempFragment);
        }


        initView();
    }

    private void initView() {

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mMenuRippleView = (RippleView) findViewById(R.id.menu_ripple_view);
        mMenuRippleView.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                mDrawerLayout.openDrawer(Gravity.RIGHT);
            }

        });

        mChannelTextView0 = (TextView) findViewById(R.id.channel_0);
        mChannelTextView1 = (TextView) findViewById(R.id.channel_1);

        mIndicatorWidth = Tools.getDisplayMetrics(this).widthPixels/2;

        mIndicator = (View) findViewById(R.id.indicator);

        mIndicatorParams = new RelativeLayout.LayoutParams(mIndicatorWidth, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
        mIndicatorParams.setMargins(0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 29, getResources().getDisplayMetrics()), 0, 0);
        mIndicator.setLayoutParams(mIndicatorParams);


        mViewPager = (ViewPager) findViewById(R.id.main_viewpager);
        mFragmentPagerAdapter = new NewsFragmentPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mFragmentPagerAdapter);
        mViewPager.setOffscreenPageLimit(2);


        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                mIndicatorParams.leftMargin = (int)((arg0 * mIndicatorWidth) + mIndicatorWidth * arg1);
                mIndicator.setLayoutParams(mIndicatorParams);
            }

            @Override
            public void onPageSelected(int arg0) {

                if (arg0 == 0) {
                    mChannelTextView0.setTextColor(getResources().getColor(R.color.channel_text_checked));
                    mChannelTextView1.setTextColor(getResources().getColor(R.color.channel_text_normal));
                } else {
                    mChannelTextView0.setTextColor(getResources().getColor(R.color.channel_text_normal));
                    mChannelTextView1.setTextColor(getResources().getColor(R.color.channel_text_checked));
                }

            }

        });
    }

    public void switchChannel(View v) {

        if(v.getId() == R.id.channel_0) {
            mViewPager.setCurrentItem(0, false);
        } else {
            mViewPager.setCurrentItem(1, false);
        }

    }

    public class NewsFragmentPagerAdapter extends FragmentStatePagerAdapter {

        public NewsFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int arg0) {
            return mArrayListFragment.get(arg0);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public int getCount() {
            return mArrayListFragment.size();
        }
    }


    public void jump(View v) {
        ImageView scene = (ImageView) findViewById(R.id.image_change);
        TransitionDrawable sceneDrawable = (TransitionDrawable) scene.getDrawable();

        sceneDrawable.reverseTransition(100);

    }
}
